define(['angularAMD', 'moment'], function (angularAMD, moment) {
    angularAMD.service('B0225Service', ['$rootScope', '$filter', '$translate', 'config', 'MMWService', '$mdDialog',
        function ($rootScope, $filter, $translate, config, $MMWService, $mdDialog) {
            this.init = function(options) {
                var nowdatetime = $filter('date')(new Date(), 'yyyy/MM/dd HH:mm:ss');
                console.log('從前一支服務傳來的options', options);
                function rollback_info_get(content, callback){
                    $MMWService.sendToServer({
                        uri : 'ServicesSTD.Module_WIP.rollback_info_get',
                        content : content,
                        msg : 'B0225.msg.rollback_info_get',
                        success : function(data){
                            if(callback)
                                callback(data);
                        }
                    });
                }

                function rollback_pre_check(content_pre, feedback) {
                    $MMWService.sendToServer({
                        uri: 'ServicesSTD.Module_WIP.rollback_pre_check',
                        content: content_pre,
                        success: function (data) {
                            feedback(data);
                        },
                        error: function () {
                            //假設後端沒更新，僅版更前端，此時回傳情境0
                            feedback('0');
                        }
                    });
                }

                function rollback_info_process(content, callback){
                    $MMWService.sendToServer({
                        uri : 'ServicesSTD.Module_WIP.rollback_info_process',
                        content : content,
                        msg : 'B0225.msg.rollback_info_process',
                        success : function(data){
                            if(callback)
                                callback(data);
                        }
                    });
                }

                function openDialog(plotItem){
                    $mdDialog.dialog(
                        'module/moduleB/program/B02/B0225/B0225.html',
                        function (dialog) {
                            return {
                                css_url:'module/moduleB/program/B02/B0225/B0225.css',
                                plotItem:plotItem,
                                back:function(){
                                    dialog.hide();
                                },
                                confirm:function(){
                                    var content = dialog.plotItem;
                                    //報工還原卡控檢查
                                    var content_pre = {
                                        LotNo: dialog.plotItem.plot_no,
                                        OpNo: dialog.plotItem.rollback_op_no,
                                        EquipmentNo: dialog.plotItem.rollback_machine_no,
                                        ReportDate: dialog.plotItem.rollback_event_time
                                    };
                                    var daily_check = '';
                                    rollback_pre_check(content_pre, function (response) {
                                        if (response) {
                                            daily_check = response;
                                            console.log("卡控情境", daily_check);
                                            //情境0、情境1可正常執行報工還原，情境2需卡控判斷，情境3需卡控且禁用
                                            if (daily_check == '2') {
                                                var msg = $filter('translate')('B0225.msg.daily_check_1');
                                                $rootScope.showConfirm(msg, function (confirm_dialog) {
                                                    confirm_dialog.hide();
                                                    //進入報工還原
                                                    //20220218 modify by 04485 for#0107478 QMS單據還原整合功能:增加QMS刪除檢驗邏輯
                                                    rollback_info_process(content, function (data) {
                                                        dialog.hide();
                                                        if (data.message != '') {
                                                            $rootScope.showAlert($filter('translate')('B0225.msg.alert1', { msg: data.message }));
                                                        } else {
                                                            $rootScope.showToast($filter('translate')('B0225.msg.success'));
                                                        }
                                                        if (options.callback)
                                                            options.callback();
                                                    });
                                                });
                                            } else if (daily_check == '3') {
                                                showAlert($filter('translate')('B0225.msg.daily_check_2'));
                                            } else {
                                                //20220218 modify by 04485 for#0107478 QMS單據還原整合功能:增加QMS刪除檢驗邏輯
                                                rollback_info_process(content, function (data) {
                                                    //69327 進站時將產線狀態改為加工
                                                    var dataChange = {};
                                                    var plan_work = {
                                                        date: nowdatetime.split(' ')[0],
                                                        time: nowdatetime.split(' ')[1]
                                                    };
                                                    var work_time = $filter('date')(plan_work.date, 'yyyy/MM/dd') + " " + plan_work.time;
                                                    dataChange = {
                                                        machine_no: plotItem.machine_no,
                                                        report_datetime: moment(new Date()).format('YYYY-MM-DD HH:mm:ss'),
                                                        machine_status: options.plotItem.status,
                                                        remark: '',
                                                        handle_user_no: config.cache.account,
                                                        plan_start_time: options.machine_status == '0' || options.machine_status == '1' ? '' : work_time,
                                                        actual_end_time: options.machine_status == '0' || options.machine_status == '1' ? work_time : '',
                                                        remarks: undefined
                                                    };
                                                    //進入報工還原
                                                    dialog.hide();
                                                    if (data.message != '') {
                                                        $rootScope.showAlert($filter('translate')('B0225.msg.alert1', { msg: data.message }));
                                                    } else {
                                                        $rootScope.showToast($filter('translate')('B0225.msg.success'));
                                                    }
                                                    if (options.callback)
                                                        options.callback(dataChange);//#69327 傳回稼動產線訊息
                                                });
                                            };
                                        }
                                    });
                                }
                            };
                        }
                    );
                }

                (function(){
                    var content = {
                        plot_no:options.plotItem.plot_no,
                        op_no:options.plotItem.op_no,
                        machine_no:options.machine_no,
                        status:options.plotItem.status == 'Q'?'0':'1'
                    };
                    if(options.parent == 'B0401'){//入庫
                        content.status = 11;
                    } else if(options.parent == 'B0216'){//出貨
                        content.status = 4;
                    } else if(options.parent == 'B0217'){//進貨
                        content.status = 5;
                    }
                    rollback_info_get(content, function(data){
                        var plotItem = data.plot_info;
                        plotItem.current_status = plotItem.status;
                        if(data.rollback_info && data.rollback_info.length > 0){
                            plotItem = angular.merge(plotItem, data.rollback_info[0]);
                            plotItem.machine_no = options.machine_no;
                            //delete plotItem.rollback_info;
                            openDialog(plotItem);
                        }
                    });
                })();
            };
        }
    ]);
});
